// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    items: [
      { name: '0', value: '学生', checked: 'true' },
      { name: '1', value: '老师' },
    ],
    username: '',
    nickname: '',
    type: 0,
    canIUse:wx.canIUse('button.open-type.getUserInfo'),
    isCheck:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //wx.clearStorageSync();
    let user = wx.getStorageSync('user')
    console.log(user)
    if(user){
      console.log('=====登录成功')
      wx.switchTab({
        url: '../home/home'
      })
    }else{
      console.log('=====开始注册')
      this.powerDrawer(null);
    }
    //wx.setStorageSync('logs', logs)
    
  },
  powerDrawer: function (e) {
    let _this = this;
    if (e != null) {
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 150000
      })
      let user = this.data;
      let length = String(user.nickname).replace(/[^\x00-\xff]/g, '__').length;
      let name = '';
      if (_this.data.isCheck) {
        let u = wx.getStorageSync('wx');
        if (u) {
          name = u.nickName
        } else {
          wx.showModal({
            title: '提示',
            content: '授权失败',
          })
          this.setData({
            canIUse: true
          })
        }
      } else {
        name = String(_this.data.nickname)
      }
      if (length > 3 && length < 11 || _this.data.isCheck) {
        wx.request({
          url: 'https://www.azhouaa.cn/wxs/User/register',
          method: 'POST',
          data: {
            "username": new Date().getTime() + '' + Math.floor(Math.random() * 10) +''+ Math.floor(Math.random() * 10),
            "nickname": name,
            "password": '123',
            "type": user.type
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res)
            wx.hideToast();
            if (res.data.code == 200) {
              wx.showToast({
                title: '注册成功',
                icon: 'success',
                duration: 1500
              })
              _this.util(e.currentTarget.dataset.statu);
              wx.setStorageSync('user', res.data.msg);
              wx.switchTab({
                url: '../home/home'
              })
            } else {
              wx.showModal({
                title: '警告',
                content: '用户名已存在',
                showCancel: false,
              })
            }
          },
          complete: function () { }
        })
      } else {
        wx.hideToast();
        wx.showModal({
          title: '警告',
          content: '请输入正确的信息',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

    } else {
      this.util('open')
    }

  },
  util: function (currentStatu) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();
    this.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.opacity(1).rotateX(0).step();
      this.setData({
        animationData: animation
      })
      //关闭    
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示    
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      type: parseInt(e.detail.value)
    })
  },
  userNameInput: function (e) {
    let val = e.detail.value;
    this.setData({
      nickname: val
    })
  },
  bindGetUserInfo: function (e) {
    let _this = this;
    wx.getSetting({
      success: function (res) {
        console.log('success')
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              wx.setStorageSync('wx', res.userInfo);
            }
          })
          _this.setData({
            canIUse: false
          })
        }else{
          wx.showModal({
            title: '请打开相应权限',
            content: '右上角菜单->关于（小程序名字）->右上角菜单->设置',
            showCancel:false
          })
        }
      },
      fail:function(){
        wx.showModal({
          title: '请打开相应权限',
          content: '右上角菜单->关于（小程序名字）->右上角菜单->设置',
          showCancel: false
        })
      },
      complete:function(){
        _this.setData({
          canIUse: false,
        })
      }
    })
  },
  checkchange:function(e){
    console.log(e.detail.value)
    if (e.detail.value.length == 1){
      this.setData({
        isCheck: true,
      })
    }else{
      this.setData({
        isCheck: false,
      })
    }
  }
})